<template>
	<view>
		<u-modal v-model="show" :show-title="false" :show-confirm-button="false">
			<div class="title">
				<p>安顺职业技术学院</p>
				<p>考证在线报名收费系统</p>
			</div>
			<template :slot="confirmButton">
				<u-button type="primary" :ripple="true" :custom-style="customStyle" @click="login" :loading="loading">登录</u-button>
			</template>
		</u-modal>

		<!-- 授权用户信息弹窗 -->
		<authorize @allowGetUserInfo="allowGetUserInfo($event)" :showbtn.sync="showbtn"></authorize>
	</view>
</template>

<script>
	import {
		getSessionInfo,
		wxLogin,
		getWxUserInfo
	} from "@/api/apiList/login.js"

	import authorize from "./components/authorize.vue"
	export default {
		components: {
			authorize
		},
		data() {
			return {
				show: true,
				loading: false,
				customStyle: {
					height: "100rpx"
				},
				encryptedData: "",
				iv: "",
				showbtn: false
			}
		},
		methods: {
			async API_getSessionInfo(code) { //获取微信openid接口
				let result = {
					param: {
						code
					}
				}
				return await getSessionInfo(result)
			},
			async API_wxLogin(openid, unionid) { //微信小程序登录接口
				let result = {
					param: {
						openid,
						unionid
					}
				}
				return await wxLogin(result)
			},
			async getWxUserInfo(sessionKey, signature, rawData, encryptedData, iv) {
				let result = {
					param: {
						sessionKey,
						signature,
						rawData,
						encryptedData,
						iv
					}
				}
				return await getWxUserInfo(result)
			},
			wxlogin() { //微信登录
				return new Promise((resolve) => {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							resolve(loginRes)
						}
					});
				})
			},
			allowGetUserInfo(e) { //授权获取用户信息
				// console.log(e)
				return e
			},
			wxGetUserInfo() { //获取用户信息
				return new Promise((resolve) => {
					uni.getUserInfo({
						provider: 'weixin',
						success: function(infoRes) {
							resolve(infoRes)
						}
					});
				})
			},
			getSetting() { //判断用户是否又授权
				return new Promise((resolve) => {
					uni.getSetting({
						success(res) {
							resolve(res)
						}
					})
				})
			},
			async login() { //登录
				const settingRes = await this.getSetting()
				const auth = settingRes.authSetting["scope.userInfo"] //判断是否有授权获取用户信息
				if (!auth) {
					this.showbtn = true //没有授权，弹出授权窗口
				} else {
					this.loading = true
					let wxloginRes = await this.wxlogin() //微信登录

					let openIdRes = await this.API_getSessionInfo(wxloginRes.code) //获取用opendId和sessionKey
					
					
					let {signature,rawData,encryptedData,iv} = await this.wxGetUserInfo() //获取用户敏感信息
					
					let getWxUserInfoRes = await this.getWxUserInfo(openIdRes.data.data.sessionKey, signature, rawData, encryptedData,iv) //调用接口-获取用户信息

					uni.setStorageSync("openid",openIdRes.data.data.openid)
					uni.setStorageSync("unionId",getWxUserInfoRes.data.data.unionId)

					let res = await this.API_wxLogin(openIdRes.data.data.openid, getWxUserInfoRes.data.data.unionId) //微信登录

					if (res.data.data.isRegister === 0) { //没注册过
						uni.setStorageSync('jwttoken', res.data.data.jwttoken);

						uni.navigateTo({ //跳转到注册页
							url: '/pages/register/register'
						});
					} else if (res.data.data.isRegister === 1) { //注册过
						uni.setStorageSync('jwttoken', res.data.data.jwttoken);

						uni.switchTab({ //跳转到首页
							url: '/pages/home/home'
						});
					}
					this.loading = false
				}

			}
		},
		async created() { //进入页面先判断用户是否有授权
			const settingRes = await this.getSetting()
			const auth = settingRes.authSetting["scope.userInfo"] //判断是否有授权获取用户信息
			if (!auth) {
				this.showbtn = true //没有授权，弹出授权窗口
			}
		}
	}
</script>

<style lang="scss" scoped>
	.title {
		padding: 60rpx;
		box-sizing: border-box;
		text-align: center;

		p {
			font-size: 40rpx;
		}
	}
</style>
